package ee.ut.cs.mc.madp.p2pdivider;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import ee.ut.cs.mc.madp.p2pdivider.fragment.DevicesFragment;
import ee.ut.cs.mc.madp.p2pdivider.fragment.UIFragment;

/**
 * @author Kaarel Jõgeva
 *
 */
public class HostActivity extends BaseDividerActivity {

    public static final String TAG = "HostActivity";

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
     * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
     * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_divider);
        /**
         * The {@link ViewPager} that will host the section contents.
         */
        // Create the adapter that will return a fragment for each of the three primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), this);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        super.onCreate(savedInstanceState);
        // Init
        // Find Peers
        findDevices(null);
    }

    public void startMedia(@SuppressWarnings("unused") View view) {
        Toast.makeText(this, "Starting to show media!", Toast.LENGTH_SHORT).show();

        propagateIPs();

    }

    public void findDevices(@SuppressWarnings("unused") View view) {
        Toast.makeText(this, R.string.searching_devices, Toast.LENGTH_SHORT).show();
        manager.discoverPeers(channel, new ActionListener() {

            @Override
            public void onSuccess() {
                Toast.makeText(HostActivity.this, "Discovery Initiated",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int reasonCode) {
                Toast.makeText(HostActivity.this, "Discovery Failed : " + reasonCode,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @SuppressWarnings("unchecked")
    public <T> T getFragment(Class<T> clazz) {
        return (T) mSectionsPagerAdapter.getItem(UIFragment.getFragment(clazz).ordinal());
    }

    @Override
    public PeerListListener getPeerListListener() {
        return getFragment(DevicesFragment.class);
    }

    public void sendStart(ArrayList<File> selectedFiles) {

    }

    @Override
    public void showFile(String name) {
        ImageView imageView = (ImageView) findViewById(R.id.dividedHostImage);

        FileInputStream image = null;
        try {
            image = openFileInput(name);

            // Get display size
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            // Bitmap decodedBitmap = Utils.decodeSampledBitmapFromResource(image, metrics.widthPixels, metrics.heightPixels, Area.N);
            // imageView.setImageBitmap(decodedBitmap);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Unable to lacate file " + name, e);
            say("Couldn't display file " + name);
        } finally {
            Utils.closeQuietly(image);
        }
    }

}
